"""
Chinese Chess Online
Mai Linh(06TC051), Nguyen The Nhan(06TC074)

Database Module
"""
from ZODB.FileStorage import FileStorage
from ZODB.DB import DB
import transaction


class ChessDatabase():
    
    def __init__(self):
        storage = FileStorage('Data.fs')
        db = DB(storage)
        connection = db.open()
        self.root = connection.root()

    def save_game(self,user,board):
        root = self.root
        if not root['save'].has_key(user):
            root['save'][user]=[]
        root['save'][user].append(board)
        transaction.commit()

    def save_highscore(self,user,highscore):
        root = self.root
        if not root['highscore'].has_key(user):
            root['highscore'][user]=[]
        root['highscore'][user].append(board)
        transaction.commit()

        

def create_database():
    storage = FileStorage('Data.fs')
    db = DB(storage)
    connection = db.open()
    root = connection.root()
    root['save']=dict()
    root['highscore']=dict()
    transaction.commit()

if __name__ == '__main__':
    #create_database()
    pass
